package showtime.framework.music
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	
	
	/** 
	 * Description:This class inherite the class of Sound, which to Inhence the function to save same usefull information.
	 * @project OceanBeans
	 * @author yinhao
	 * @date 2011-3-13
	 *
	 */
	
	public class Music
	{
		private var sound:Sound = null;
		
		private var looped:Boolean = false;
		private var channel:SoundChannel = null;
		
		public function loadClass(clazz:Class, looped:Boolean = false):Music
		{
			this.looped = looped;
			sound = new clazz();
			return this;
		}
		
		public function stop():void
		{
			if (channel != null)
			{
				channel.stop();
				channel = null;
			}
		}
		
		public function play(restart:Boolean = false):void
		{
			if (restart)
			{
				stop();
			}
			
			channel = sound.play(0, int.MAX_VALUE);
			
			if (channel != null && looped)
			{
				channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			}
		
		}
		
		private function onSoundComplete(event:Event):void
		{
			if (channel == null)
			{
				return;
			}
			
			channel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete);
			looped && play();
		}
		
		static public function stopAll():void
		{	
			SoundMixer.stopAll();			
		}
		
		static public function setVolume(value:Number):void
		{
			SoundMixer.soundTransform = new SoundTransform(value);			
		}
	}
}